package site.marqstree.kotlin.library.good.ui.fragment.category.centent

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import kotlinx.android.synthetic.main.fragment_category_content.*
import site.marqstree.kotlin.library.base.ui.fragment.BaseMvpFragment
import site.marqstree.kotlin.library.good.R
import site.marqstree.kotlin.library.good.ui.fragment.category.centent.bean.CategoryContentBean
import site.marqstree.kotlin.library.good.ui.fragment.category.centent.inject.DaggerContentComponent
import site.marqstree.kotlin.library.good.ui.fragment.category.centent.presenter.ContentPresenter
import site.marqstree.kotlin.library.good.ui.fragment.category.centent.recyclerview.ContentAdapter
import site.marqstree.kotlin.library.good.ui.fragment.category.centent.view.ContentView
import site.marqstree.kotlin.library.good.ui.fragment.category.list.listener.ICategoryList


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.library.good.ui.fragment.category.centent
 * 文件名: ContentFragment
 * 创建者: marqstree
 * 创建时间: 2020/3/7 11:13
 * 描述: TODO
 */
class ContentFragment():BaseMvpFragment<ContentPresenter>(),
                    ContentView,
                    ICategoryList{

    private val mAdapter = ContentAdapter(this)
    var mCategoryId: Int = 0

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return inflater.inflate(R.layout.fragment_category_content,null)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //初始化RecyclerView
        initRecyclerView()
    }

    //初始化RecyclerView
    private fun initRecyclerView(){
        mRvCategoryContent.layoutManager = GridLayoutManager(context, 2)
        mRvCategoryContent.scheduleLayoutAnimation()
        mRvCategoryContent.adapter = mAdapter
    }

    override fun initBusinessInjection() {
        DaggerContentComponent.builder()
            .activityComponent(activityComponent)
            .build()
            .inject(this)
        mPresenter.mView = this
    }

    override fun onContentResult(list: List<CategoryContentBean>) {
        mAdapter.setNewData(list.toMutableList())
    }

    override fun changeCategoryId(categoryId: Int) {
        mCategoryId = categoryId
        mPresenter.getCategoryContent(mCategoryId)
    }
}